package com.etc.controller;

import java.util.List;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.etc.entity.Attractions;
import com.etc.entity.Scenic;
import com.etc.service.AttractionsService;
import com.etc.service.ScenicService;
import com.etc.util.Message;
import com.etc.util.PageData;

/**
 * cyf控制器
 * 
 * @author Administrator
 *
 */

@Controller
@RequestMapping("attapi")
public class AttrationsController {
	// 注入service实现层
	@Resource(name = "attrationsService")
	private AttractionsService attrationsService;
	@Resource
	private ScenicService ss;
	
	Logger log = Logger.getLogger(AttrationsController.class);

	@RequestMapping(value = "insertScenic", method = RequestMethod.POST)
	public String insertScenic(@RequestBody Scenic scenic) {
		log.info("insertScenic方法");
		log.info(scenic.toString());
		ss.insertScenic(scenic);
		return null;
	}

	// 添加景点
	@RequestMapping(value = "insertAttrations", method = RequestMethod.POST)
	@ResponseBody
	public Message insertAttrations(@RequestBody Attractions attractions) {
		Message msg = new Message();
//		String attprice=Double.toString(attractions.getAttprice());
//		if(attprice.matches("^[0-9]+$")) {
		boolean flag = attrationsService.insert(attractions);

		if (flag) {
			msg.setMsg("添加成功");
		} else {
			msg.setMsg("添加失败");
		}
		return msg;
//		}else {
//			msg.setMsg("输入格式有误");
//			return msg;
//		}

	}

	// 根据id删除景点信息
	@RequestMapping(value = "deletAttrations/{attid}", method = RequestMethod.DELETE)
	@ResponseBody
	public Message deletAttrations(@PathVariable(value = "attid") int attid) {

		boolean flag = attrationsService.deleteByPrimaryKey(attid);
		Message msg = new Message();
		if (flag) {
			msg.setMsg("删除成功");
		} else {
			msg.setMsg("删除失败");
		}
		return msg;
	}

	// 根据id修改景点信息
	@RequestMapping(value = "updateAttrations", method = RequestMethod.PUT)
	@ResponseBody
	public Message updateAttrations(@RequestBody Attractions attractions) {
		boolean flag = attrationsService.updateByPrimaryKeySelective(attractions);
		Message msg = new Message();
		if (flag) {
			msg.setMsg("更新成功");
		} else {
			msg.setMsg("更新失败");
		}
		return msg;
	}

	// 根据id获取景点由前端页面加载相关页面时就请求此控制器
	@RequestMapping(value = "selectAttrations/{attid}", method = RequestMethod.GET)
	@ResponseBody
	public Attractions selectAttrationsByid(@PathVariable(value = "attid") int attid) {
		Attractions attractions = attrationsService.selectByPrimaryKey(attid);
		return attractions;
	}

	// 查询所有景点信息
	@RequestMapping(value = "selectAll", method = RequestMethod.GET)
	@ResponseBody
	public List<Attractions> selectAll() {
		List<Attractions> list = attrationsService.selectAll();
		return list;
	}

	// 分页+模糊查询风景信息
	@RequestMapping(value = "selectBypage", method = RequestMethod.GET)
	@ResponseBody
	public PageData<Attractions> selectBypage(int page, int limit, String context) {
		PageData<Attractions> data = attrationsService.selectByPage(page, limit, context);
		return data;
	}

}
